package fi.honkalampisaatio.sound;

import java.util.HashMap;

import fi.honkalampisaatio.game.utilities.TextToSpeech;

public class VoicePlayer {
	SoundPlayer soundPlayer;
	static VoicePlayer INSTANCE = null;
	HashMap<String, SoundPlayer> voiceMap;
	boolean isOn;


	public static VoicePlayer getInstance() {
		if(INSTANCE==null)
			INSTANCE = new VoicePlayer();
		return INSTANCE;
	}
	
	public VoicePlayer()
	{
		soundPlayer = null;
		voiceMap = new HashMap<String, SoundPlayer>();
		isOn = true;
	}
	
	public String addVoice(String filename)
	{
		if(voiceMap.containsKey(filename))
		{
			return filename;
		}
		String key = null;
		if(filename!=null && !filename.isEmpty())
		{
			SoundPlayer player = new SoundPlayer();
			if(player.setSoundFile(filename))
			{
				voiceMap.put(filename, player);
				key = filename;
			}
		}
		return key;
	}
	
	public String addTTSVoice(String sentence)
	{
		if(voiceMap.containsKey(sentence))
		{
			return sentence;
		}
		SoundPlayer player = TextToSpeech.getInstance().getSoundPlayer(sentence);
		if(player!=null)
		{
			voiceMap.put(sentence, player);
			return sentence;
		}
		else
		{
			return null;
		}
	}
	
	public void play(String key)
	{
		if(soundPlayer!=null)
		{
			soundPlayer.stop();
		}
		soundPlayer = voiceMap.get(key);
		if(soundPlayer!=null && isOn)
		{
			soundPlayer.play();
		}
	}
	
	public void setOn(boolean isOn)
	{
		this.isOn = isOn;
		if(soundPlayer!=null && !isOn)
		{
			soundPlayer.stop();
		}
	}

	public boolean isOn() {
		return isOn;
	}

}
